#include "mainscreen.h"

char * dom;
char * univ;
char * login;
char * pwd;

int textlayer =2;



void clearMainScreen(){
	NF_DeleteSprite(1,0);
	NF_DeleteSprite(1,1);
	NF_DeleteSprite(1,2);
	NF_DeleteSprite(1,3);
	NF_DeleteSprite(1,4);
	NF_DeleteSprite(1,5);
	NF_DeleteSprite(1,6);
	NF_DeleteSprite(1,7);
	NF_DeleteSprite(1,8);

	//NF_ClearTextLayer(1, 0);
	NF_UpdateTextLayers();
	oamUpdate(&oamMain);
	oamUpdate(&oamSub);

	NF_DeleteTextLayer(1, textlayer);
	//NF_UnloadTextFont("normaltop");

	
	NF_DeleteTiledBg(1, 3);
	NF_SpriteOamSet(0);
	NF_SpriteOamSet(1);
	swiWaitForVBlank();
	oamUpdate(&oamMain);
	oamUpdate(&oamSub);
}

void createMainScreen(){
	drawBackground();
	drawText();
	drawButton();
	drawArrows();

	NF_SpriteOamSet(0);
	NF_SpriteOamSet(1);
	swiWaitForVBlank();
	oamUpdate(&oamMain);
	oamUpdate(&oamSub);
}


void loadDataMainScreen(){
	//BackgroundLoading
	NF_LoadTiledBg(	"bgmenu",	// Nombre de archivo
					"bgmenu",		// Nombre que le das al fondo
					256,		// Ancho del fondo en pixeles
					256			// Alto del fondo en pixeles
					);


	
	NF_LoadTiledBg(	"splash",	// Nombre de archivo
					"splash",		// Nombre que le das al fondo
					256,		// Ancho del fondo en pixeles
					256			// Alto del fondo en pixeles
					);


	//Sprite loading
	NF_LoadSpriteGfx("boutons", 0, 64, 32);//fname, id slot gfx 0-255, width, height
	NF_LoadSpritePal("boutons", 0);
	
	NF_LoadSpriteGfx("arrows", 1, 32, 32);//fname, id slot gfx 0-255, width, height
	NF_LoadSpritePal("arrows", 1);

	/*NF_LoadSpriteGfx("next", 2, 32, 32);//fname, id slot gfx 0-255, width, height
	NF_LoadSpritePal("next", 2);*/

	
	//pal & sprite des button
		//1 seule palette
	NF_VramSpritePal(1, 0, 0);
		//gfx (4 boutons)
	NF_VramSpriteGfx(1, 0, 0, true);//mise en ram 0 du gfx 0	
	NF_VramSpriteGfx(1, 0, 1, true);//mise en ram 1 du gfx 0
	NF_VramSpriteGfx(1, 0, 2, true);//mise en ram 2 du gfx 0
	NF_VramSpriteGfx(1, 0, 3, true);//mise en ram 3 du gfx 0


	//pal & sprite des fleches	
	NF_VramSpritePal(1, 1, 1);//screen,ramslot,vramslot
	NF_VramSpriteGfx(1, 1, 4, true);//screen,ramslot,vramslot,frame
	NF_VramSpriteGfx(1, 1, 5, true);//screen,ramslot,vramslot,frame
	NF_VramSpriteGfx(1, 1, 6, true);//screen,ramslot,vramslot,frame
	NF_VramSpriteGfx(1, 1, 7, true);//screen,ramslot,vramslot,frame
	NF_VramSpriteGfx(1, 1, 8, true);//screen,ramslot,vramslot,frame

	//bouton valider
	/*NF_VramSpritePal(1, 2, 2);//screen,ramslot,vramslot
	NF_VramSpriteGfx(1, 2, 8, false);//screen,ramslot,vramslot,frame*/


	//texte 
	NF_LoadTextFont("default",	// file name (.FNT)
					"normal",	// name
					256,
					256,
					0			// rotation
					);

	NF_LoadTextFont("default",	// file name (.FNT)
					"normaltop",	// name
					256,
					256,
					0			// rotation
					);
}

void drawText(){

	/*NF_LoadTextFont("default",	// file name (.FNT)
					"normaltop",	// name
					256,
					256,
					0			// rotation
					);*/

	NF_CreateTextLayer(1,		// screen 0-1
					textlayer,			// layer 0-3
					0,			// rotation 0-2
					"normaltop"	// name
					);


	
	NF_UpdateTextLayers();			// Actualiza las capas de texto
}

void drawBackground(){
	NF_CreateTiledBg(	1,		// screen
						3,		// layer 0-3
						"bgmenu"	// name
						);
	NF_CreateTiledBg(	0,		// screen
						3,		// Numero de capa donde colocaras el fondo
						"splash"	// Nombre del fondo
						);
}

void drawButton(){
	//NF_SpriteLayer(1,0,3);
	NF_CreateSprite(1, //screen
			 0, //sprite id
			 0, //gfx
			 0, //palette
			 96, 16);//x,y
	NF_WriteText(1, textlayer, 12, 3, "country");

	NF_CreateSprite(1, //screen
			 1, //sprite id
			 1, //gfx
			 0, //palette
			 96, 56);//x,y
	NF_WriteText(1, textlayer, 12, 8, "univers");

	NF_CreateSprite(1, //screen
			 2, //sprite id
			 2, //gfx
			 0, //palette
			 96, 96);//x,y
	NF_WriteText(1, textlayer, 12, 13, "            ");
	if(strlen(login) > 0)
		NF_WriteText(1, textlayer, 12, 13, login);
	else
		NF_WriteText(1, textlayer, 12, 13, "login");

	NF_CreateSprite(1, //screen
			 3, //sprite id 0-127
			 3, //gfx
			 0, //palette
			 96, 136);//x,y
	NF_WriteText(1, textlayer, 12, 18, "            ");
	
	if(strlen(pwd) > 0)
		NF_WriteText(1, textlayer, 12, 18, "******");
	else
		NF_WriteText(1, textlayer, 12, 18, "pwd");
		
	NF_SpriteLayer(1,0,3);//screen,idsprite, layer
	NF_SpriteLayer(1,1,3);
	NF_SpriteLayer(1,2,3);
	NF_SpriteLayer(1,3,3);

	

	NF_UpdateTextLayers();
}


void drawArrows(){
	NF_CreateSprite(1, //screen
				4, //sprite id 0-127
				4, //gfx
				1, //palette
				54, 13);//x,y
	
	NF_CreateSprite(1, //screen
				5, //sprite id 0-127
				5, //gfx
				1, //palette
				170, 13);//x,y
	
	NF_HflipSprite(1,5,true);//screen, sprite,boolvflip
	
	NF_CreateSprite(1, //screen
				6, //sprite id 0-127
				6, //gfx
				1, //palette
				54, 53);//x,y
	
	NF_CreateSprite(1, //screen
				7, //sprite id 0-127
				7, //gfx
				1, //palette
				170, 53);//x,y
	
	NF_HflipSprite(1,7,true);//screen, sprite,boolvflip
	
	//bouton connect
	NF_CreateSprite(1, //screen
				8, //sprite id 0-127
				8, //gfx
				1, //palette
				200, 136);//x,y		
	
	NF_HflipSprite(1,8,true);
	
	//NF_WriteText(1, textlayer, 23, 20, "connect");
	NF_UpdateTextLayers();

	NF_SpriteLayer(1,4,3);//screen,idsprite, layer
	NF_SpriteLayer(1,5,3);
	NF_SpriteLayer(1,6,3);
	NF_SpriteLayer(1,7,3);
	NF_SpriteLayer(1,8,3);

	/*NF_CreateSprite(1, //screen
			 8, //sprite id 0-127
			 8, //gfx
			 2, //palette
			 196, 136);//x,y         */

}


OverviewPage * boutonTouched(){
	touchPosition touch;
	touchRead(&touch);
	int x = touch.px;
	int y = touch.py;

	/*char mytext[32];
	sprintf(mytext,"(%d,%d)", x,y);
	NF_WriteText(0, 0, 0, 5, mytext);//screen, layer,x,y,text
	NF_UpdateTextLayers();*/
	if((x > 96) &&  (x < (96 + 64))){
		/*if((y > 12) && (y < (12 +32))){
			NF_SpriteFrame(1, 0, 1);
			NF_SpriteFrame(1, 1, 0);
			NF_SpriteFrame(1, 2, 0);
			NF_SpriteFrame(1, 3, 0);
			//NF_WriteText(0, 0, 0, 6, "ok1");
		}
		if((y > 54) && (y < (54 +32))){
			NF_SpriteFrame(1, 1, 1);
			NF_SpriteFrame(1, 0, 0);
			NF_SpriteFrame(1, 2, 0);
			NF_SpriteFrame(1, 3, 0);
			//NF_WriteText(0, 0, 0, 7, "ok2");
		}*/
		if((y > 96) && (y < (96 +32))){
			NF_SpriteFrame(1, 2, 1);
			NF_SpriteFrame(1, 1, 0);
			NF_SpriteFrame(1, 0, 0);
			NF_SpriteFrame(1, 3, 0);
			
			clearMainScreen();
			if(login != 0)
					free(login);
			login = inputkey("What is your name?");
			createMainScreen();
			
			
		}
		if((y > 138) && (y < (138 +32))){
			NF_SpriteFrame(1, 3, 1);
			NF_SpriteFrame(1, 1, 0);
			NF_SpriteFrame(1, 2, 0);
			NF_SpriteFrame(1, 0, 0);
			clearMainScreen();
			pwd = inputkey("What is your password?");
			createMainScreen();
		}
	}
	if(x >= 200 && y >= 136 && x <= 232 && y <= 168){
			
			NF_WriteText(0, 0, 0, 20, "connect      ");
			NF_WriteText(0, 0, 0, 21, "           ");
			NF_UpdateTextLayers();
			ConnectDialog *d = new ConnectDialog();
			d->show();

 			loginview(univ, dom,login,pwd);
			//loginview("40", "org","blinkme","coincoin");
			

			OverviewPage *f = NULL;
			//f = testParseLoginResponse();
			if( lastdata != NULL)
				f=parseLoginResponse(lastdata);
			if(f!=NULL)
				f->toString();

			d->hide();
			delete d;

			/*if(f!=NULL)
				delete f;*/
			
			NF_WriteText(0, 0, 0, 21, "fin           ");
			NF_UpdateTextLayers();
			
			return f;
	}
	 return NULL;

}


void arrowTouched(){
	touchPosition touch;
	touchRead(&touch);
	int x = touch.px;
	int y = touch.py;
	if((x > 54) &&  (x < (54 + 32))){
		//-- domaine
		if((y > 12) && (y < (12 +32))){
			NF_SpriteFrame(1, 4, 1);
			NF_SpriteFrame(1, 5, 0);
			NF_SpriteFrame(1, 6, 0);
			NF_SpriteFrame(1, 7, 0);
			
			
			int max = domsize() /*/ NBDOM*/;

			/*char mytext[8];
			sprintf(mytext,"(%d,%d)", curdom,max);
			NF_WriteText(0, 0, 0, 7, mytext);//screen, layer,x,y,text*/
			char bttext[8];
			curdom--;
			if(curdom < 0)
				curdom = max-1;
			
			sprintf(bttext,"   %s  ", DOM2[curdom]);
			dom = DOM2[curdom];
			NF_WriteText(1, textlayer, 12, 3, bttext);
			NF_UpdateTextLayers();
		}
		//-- univers
		if((y > 52) && (y < (52 +32))){
			NF_SpriteFrame(1, 4, 0);
			NF_SpriteFrame(1, 5, 0);
			NF_SpriteFrame(1, 6, 1);
			NF_SpriteFrame(1, 7, 0);
			curuniv--;
			int max = univsize() /*/ NBUNIV*/;
			char bttext[8];
			if(curuniv < 0)
				curuniv = max-1;
			
			sprintf(bttext,"   %s  ", UNIV[curuniv]);	
			univ = UNIV[curuniv];
			NF_WriteText(1, textlayer, 12, 8, bttext);
			NF_UpdateTextLayers();
			
		}
	}
	if((x > 170) &&  (x < (170 + 32))){
		//++ dom
		if((y > 12) && (y < (12 +32))){
			NF_SpriteFrame(1, 4, 0);
			NF_SpriteFrame(1, 5, 1);
			NF_SpriteFrame(1, 6, 0);
			NF_SpriteFrame(1, 7, 0);
			
			int max = domsize() /*/ NBDOM*/;
			/*char mytext[32];
			sprintf(mytext,"(%d,%d)", curdom,max);
			NF_WriteText(0, 0, 0, 7, mytext);//screen, layer,x,y,text*/
			char bttext[8];
			curdom++;
			if(curdom >= max)
				curdom = 0;
			sprintf(bttext,"   %s  ", DOM2[curdom]);
			dom = DOM2[curdom];
			NF_WriteText(1, textlayer, 12, 3, bttext);
			NF_UpdateTextLayers();
			
		}
		//++ univ
		if((y > 52) && (y < (52 +32))){
			NF_SpriteFrame(1, 4, 0);
			NF_SpriteFrame(1, 5, 0);
			NF_SpriteFrame(1, 6, 0);
			NF_SpriteFrame(1, 7, 1);
			curuniv++;
			int max = univsize() /*/ NBUNIV*/;
			char bttext[8];
			if(curuniv >= max)
				curuniv = 0;
			
			sprintf(bttext,"   %s  ", UNIV[curuniv]);
			univ = UNIV[curuniv];
			NF_WriteText(1, textlayer, 12, 8, bttext);
			NF_UpdateTextLayers();
			
		}
	}
}

